﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySoldier : EnemyBaseMovable
{
    private void Awake()
    {
        base.Init();
    }

    private void FixedUpdate()
    {
        base.CommonFixedUpdate();
    }
    public override Vector2 GetBodyPartPosition(Person.BodyPart bodyPart)
    {
        Vector2 ret;
        switch (bodyPart)
        {
            case Person.BodyPart.Foot:
                ret = base.GetBodyPartPosition(bodyPart);
                break;
            case Person.BodyPart.Head:
                ret = transform.position + 0.75f * Vector3.up + (0.4375f * lookAt.x * Vector3.right);
                break;
            case Person.BodyPart.Body:
                ret = transform.position;
                break;
            default:
                throw new System.NotImplementedException();
        }
        return ret;
    }

    public override void Run()
    {
        if (!running)
        {
            base.Run();

            Player player = null;
            Player[] players = GameObject.FindObjectsOfType<Player>().Filter((Player player) =>
            {
                return player.gameObject.activeInHierarchy && player.GetLife() > 0;
            }).ToArray();
            if (players.Length > 0)
            {
                player = players[Random.Range(0, players.Length)];
            }

            float dirX;
            if (player != null)
            {
                dirX = player.transform.position.x - transform.position.x;
            }
            else
            {
                dirX = Camera.main.transform.position.x - transform.position.x;
            }
            lookAt = dirX >= 0 ? Vector2.right : Vector2.left;

        }
    }
}
